//
//  DGGDateConvert.m
//  DGGCommonSDK
//
//  Created by dgg_hzk on 2019/11/20.
//

#import "DGGDateConvert.h"
#import "NSString+Extension.h"
#import "NSDate+Extension.h"

@implementation DGGDateConvert

+(NSString *)compareCurrentTimeTransformationWithUnitTime:(NSString *)unitTime{
    return [self compareCurrentTime:[NSDate date] transformationWithUnitTime:unitTime];
}

+ (NSString *)compareCurrentTime:(NSDate *)currentTime transformationWithUnitTime:(NSString *)unitTime{
    return [self compareCurrentTime:currentTime transformationWithUnitTime:unitTime space:30];
}

+ (NSString *)compareCurrentTime:(NSDate *)currentTime transformationWithUnitTime:(NSString *)unitTime space:(NSInteger)space{
    NSTimeInterval nowTime = [currentTime timeIntervalSince1970];
    CGFloat time = unitTime.length>11?[unitTime floatValue]/1000:[unitTime floatValue];
    NSInteger gapTime = nowTime -time;
    if (gapTime <25)
    {
        return @"刚刚";
    }else if (gapTime <60)
    {
        return [NSString stringWithFormat:@"%ld秒前",(long)gapTime];
    }else if (gapTime <60*60)
    {
        return [NSString stringWithFormat:@"%ld分钟前",(long)gapTime/60];
    }else if (gapTime <60*60*24)
    {
        return [NSString stringWithFormat:@"%ld小时前",(long)gapTime/60/60];
    }else if (gapTime <60*60*24*space)
    {
        return [NSString stringWithFormat:@"%ld天前",(long)gapTime/60/60/24];
    }else
    {
        return [NSDate unixTimeStampToDateTimeStr:unitTime format:@"YYYY-MM-dd"];
    }
}

+(NSString *)comparePreciseTimeWithTime:(NSString *)time format:(NSString *)format{
    if ([NSString isEmpty:format]) {
        format = @"YYYY-MM-dd HH:mm:ss";
        time =  [NSDate unixTimeStampToDateTimeStr:time format:format];
    }
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit =  NSCalendarUnitYear|NSCalendarUnitDay;
    NSDate *fromDate = [NSDate getDateFormat:format dataStr:time];
    
    NSDateComponents *startCom =    [calendar components:unit fromDate:fromDate];
    NSDateComponents *endCom =    [calendar components:unit fromDate:[NSDate date]];
    
    if (endCom.day == startCom.day &&endCom.year == startCom.year ) {
        return [NSDate getDateString:@"HH:mm" date:fromDate];
    }else if (endCom.day != startCom.day &&endCom.year == startCom.year )
    {
        return [NSDate getDateString:@"MM月dd日 HH:mm" date:fromDate];
    }else if (endCom.day != startCom.day &&endCom.year != startCom.year )
    {
        return [NSDate getDateString:@"YYYY年MM月dd日 HH:mm" date:fromDate];
    }else
    {
        return [NSDate getDateString:@"YYYY年MM月dd日 HH:mm" date:fromDate];
    }
}

+ (NSString *)compareCurrentTimeAndHourWithTimeString:(NSString *)string{
    //    NSString *nowStr = @"2016-01-01 10:20:20";
    //    NSDate *nowDate = [Helper getDateFormat:@"yyyy-MM-dd HH:mm:ss" dataStr:nowStr];
    //
    //    string = @"2018-2-25 10:10:10";
    
    //当前时间
    NSDate *nowDate = [NSDate date];
    //增加时分的时间格式
    NSString *hourAndMin = [NSDate getDateString:@"yyyy-MM-dd HH:mm:ss" dataStr:string enddateTimeFormat:@" HH:mm"];
    //时间格式器
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设置时间格式器的时间格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormatter setAMSymbol:@"上午"];
    [dateFormatter setPMSymbol:@"下午"];
    
    //时间字符串转时间
    NSDate *timeDate = [dateFormatter dateFromString:string];
    
    //当前日历
    NSCalendar *calendar = [NSCalendar currentCalendar];
    //设置日历以星期一为开始
    calendar.firstWeekday = 2;
    //日历单元(年,月,周,日,时间所在年的周数)
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekOfYear | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday;
    //当前时间部件
    NSDateComponents *nowComponents = [calendar components:calendarUnit fromDate:nowDate];
    //目标时间部件
    NSDateComponents *timeComponents = [calendar components:calendarUnit fromDate:timeDate];
    
    //当前时间与目标时间的时间间隔
    NSTimeInterval timeInterval = [nowDate timeIntervalSinceDate:timeDate];
    
    //结果字符串
    NSString *result;
    
    //当前时间在目标时间之后并且两者在同一周
    if (timeInterval > 0 && timeComponents.weekOfYear == nowComponents.weekOfYear) {
        if (timeComponents.day == nowComponents.day) {//同一天(今天)
            
            //获取系统是24小时制或者12小时制
            NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
            NSRange containsA =[formatStringForHours rangeOfString:@"a"];
            BOOL hasAMPM = containsA.location != NSNotFound;
            [dateFormatter setDateFormat:hasAMPM ? @"ah:mm" : @"HH:mm"];
            result = [dateFormatter stringFromDate:timeDate];
            
        } else {
            
            NSTimeInterval yesterdayTimeInterval = (24+nowComponents.hour)*60*60 + nowComponents.minute*60 + nowComponents.second;
            if (timeInterval <= yesterdayTimeInterval) {//昨天
                result = [NSString stringWithFormat:@"昨天%@",hourAndMin];
            } else {
                NSArray *weekArray = @[@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"];
                result = [NSString stringWithFormat:@"%@%@",weekArray[timeComponents.weekday-1],hourAndMin];
            }
        }
        
    } else {
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        result = [dateFormatter stringFromDate:timeDate];
    }
    
    return result;
}

+ (NSString *)compareCurrentTimeWithTimeString:(NSString *)string{
    //当前时间
    NSDate *nowDate = [NSDate date];
    
    //时间格式器
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设置时间格式器的时间格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormatter setAMSymbol:@"上午"];
    [dateFormatter setPMSymbol:@"下午"];
    
    //时间字符串转时间
    NSDate *timeDate = [dateFormatter dateFromString:string];
    
    //当前日历
    NSCalendar *calendar = [NSCalendar currentCalendar];
    //设置日历以星期一为开始
    [calendar setFirstWeekday:2];
    //日历单元(年,月,周,日,时间所在年的周数)
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekOfYear | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday;
    //当前时间部件
    NSDateComponents *nowComponents = [calendar components:calendarUnit fromDate:nowDate];
    //目标时间部件
    NSDateComponents *timeComponents = [calendar components:calendarUnit fromDate:timeDate];
    //结果字符串
    NSString *result;
    //当前时间与目标时间的时间间隔
    NSTimeInterval timeInterval = [nowDate timeIntervalSinceDate:timeDate];
    //当前时间在目标时间之后并且两者在同一周
    if (timeInterval > 0 && timeComponents.weekOfYear == nowComponents.weekOfYear) {
        if (timeComponents.day == nowComponents.day) {//同一天(今天)
            
            //获取系统是24小时制或者12小时制
            NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
            NSRange containsA =[formatStringForHours rangeOfString:@"a"];
            BOOL hasAMPM = containsA.location != NSNotFound;
            [dateFormatter setDateFormat:hasAMPM ? @"ah:mm" : @"HH:mm"];
            result = [dateFormatter stringFromDate:timeDate];
            
        } else {
            
            NSTimeInterval yesterdayTimeInterval = (24+nowComponents.hour)*60*60 + nowComponents.minute*60 + nowComponents.second;
            if (timeInterval <= yesterdayTimeInterval) {//昨天
                result = @"昨天";
            } else {
                NSArray *weekArray = @[@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"];
                result = weekArray[timeComponents.weekday-1];
            }
        }
    } else {
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        result = [dateFormatter stringFromDate:timeDate];
    }
    return result;
}

@end
